var DropdownUtil = {
  methods: {
    setDeptWorkshopData: function (treeObj, user, type, parentObj) {
      var id, name, dataId;
      switch (type) {
        case 'dept':
          id = user.deptId;
          dataId = 'dept_' + user.deptId;
          name = user.deptName;
          break;
        case 'workshop':
          id = user.workShopId;
          dataId = 'workshop_' + user.workShopId;
          name = user.workShopName;
          break;
        case 'user':
          id = user.userId;
          dataId = 'user_' + user.userId;
          name = user.userName;
          break;
      }

      if (name === '') {
        return null;
      }

      var obj = treeObj[dataId];
      if (obj !== undefined) {
        return obj;
      }

      var parentId = null;
      if (!SysUtil.isEmpty(parentObj)) {
        parentId = parentObj.dataId;
      }
      obj = {
        id: id,
        type: type,
        dataId: dataId,
        name: name,
        parentId: parentId
      };
      treeObj[dataId] = obj;
      return obj;
    }
  },

  getMaterialList: function () {
    return ajaxRequest('basicData/getCompanyMaterialName').then(function (materialList) {
      materialList = _.filter(materialList, function (material) {
        return material !== null && material.trim() !== '';
      });
      materialList = _.uniq(materialList);
      return materialList;
    }).catch(function () {
      return [];
    });
  },

  getModelList: function () {
    return ajaxRequest('basicData/getCompanyModel').then(function (modelList) {
      modelList = _.filter(modelList, function (model) {
        return model !== null && model.trim() !== '';
      });
      modelList = _.uniq(modelList);
      return modelList;
    }).catch(function () {
      return [];
    });
  },

  getColorList: function () {
    return ajaxRequest('basicData/getCompanyColor').then(function (colorList) {
      colorList = _.filter(colorList, function (color) {
        return color !== null && color.trim() !== '';
      });
      colorList = _.uniq(colorList);
      return colorList;
    }).catch(function () {
      return [];
    });
  },

  getCustomerNameList: function () {
    return ajaxRequest('basicData/getCustomerName', {
      view: 1
    }).then(function (customerList) {
      if (customerList === null) {
        customerList = [];
      }

      var customerNameList = _.map(customerList, 'customerName');
      customerNameList = _.filter(customerNameList, function (customerName) {
        return customerName !== null && customerName.trim() !== '';
      });
      return customerNameList;
    }).catch(function () {
      return [];
    });
  },

  getCustomerNameAndCodeList: function (postData) {
    return ajaxRequest('basicData/getCustomerCode', postData).then(function (customerList) {
      customerList = _.map(customerList, function (customer) {
        return {
          customerCode: customer.customerCode,
          customerName: customer.customerName
        };
      });
      return customerList;
    });
  },

  getMaterialTypeList: function () {
    return ajaxRequest('companyMaterialType/getCompanyMaterialTypeListData', {
      status: 1
    }).then(function (materialTypeList) {
      var materialTypeGroup = _.groupBy(materialTypeList, 'defaultWarehouse');
      var materialType = {};
      var typeObj = {
        "1": "原材料仓",
        "2": "辅料仓"
      };
      for (var key in materialTypeGroup) {
        if (typeObj[key] === undefined) {
          continue;
        }
        var materialTypes = materialTypeGroup[key];
        materialTypes = _.map(materialTypes, 'materialType');
        materialType[typeObj[key]] = materialTypes;
      }
      return materialType;
    });
  },

  getPublicMaterialTypeList: function () {
    return ajaxRequest('basicData/getRowMaterialType', {
      isvolid: 1
    }).then(function (materialTypeList) {
      materialTypeList = _.filter(materialTypeList, function (materialType) {
        return materialType !== null && materialType.trim() !== '';
      });
      materialTypeList = _.uniq(materialTypeList);
      return materialTypeList;
    }).catch(function () {
      return [];
    });
  },

  getUnitList: function () {
    return ajaxRequest('basicData/getCompanyUnit').then(function (unitList) {
      unitList = _.filter(unitList, function (unit) {
        return unit !== null && unit.trim() !== '';
      });
      unitList = _.uniq(unitList);
      return unitList;
    }).catch(function () {
      return [];
    });
  },

  getOrderTypeList: function () {
    return ajaxRequest('order/getOrderType').then(function (orderTypeList) {
      orderTypeList = _.filter(orderTypeList, function (orderType) {
        return orderType !== null && orderType.trim() !== '';
      });
      orderTypeList = _.uniq(orderTypeList);
      return orderTypeList;
    }).catch(function () {
      return [];
    });
  },

  getReceiptPaymentRecordCreateUserList: function (createUserList) {
    if (createUserList !== undefined) {
      return Promise.resolve(null);
    }
    return ajaxRequest('warehouseInSumMoney/getReceiptPaymentRecordUser').then(function (createUserList) {
      createUserList = _.map(createUserList, function (user) {
        return {
          id: user.userId,
          name: user.userName
        };
      });
      return createUserList;
    });
  },

  getReceiptPaymentRecordMode: function (modeList) {
    if (modeList !== undefined) {
      return Promise.resolve(null);
    }
    return ajaxRequest('warehouseInSumMoney/getPaymentModeList').then(function (modeList) {
      modeList = _.uniq(modeList);
      return modeList;
    });
  },

  getMaterialSelectData: function (dataList, type) {
    if (dataList !== undefined) {
      return Promise.resolve(null);
    }
    return ajaxRequest('materialSelectFilter/getMaterialSelectData', {
      type: type
    }).then(function (customerNameList) {
      customerNameList = _.uniq(customerNameList);
      return customerNameList;
    });
  },

  getDeptWorkshopUserList: function () {
    var setDeptWorkshopData = this.methods.setDeptWorkshopData;
    return ajaxRequest("user/getUserByDeptAndWork").then(function (userList) {
      var deptWorkshopObj = {};
      var deptWorkshopList = [];
      for (var i = 0; i < userList.length; i++) {
        var user = userList[i];
        var dept = setDeptWorkshopData(deptWorkshopObj, user, 'dept');
        var workshop = setDeptWorkshopData(deptWorkshopObj, user, 'workshop', dept);
        if (workshop === null) {
          workshop = dept;
        }
        setDeptWorkshopData(deptWorkshopObj, user, 'user', workshop);
      }

      for (const dataId in deptWorkshopObj) {
        var deptWorkshop = deptWorkshopObj[dataId];
        var parentId = deptWorkshop.parentId;
        if (parentId !== null) {
          var parentIdObj = deptWorkshopObj[parentId];
          if (!parentIdObj) {
            continue;
          }
          var children = parentIdObj.children;
          if (children === undefined) {
            children = [];
            parentIdObj.children = children;
          }
          children.push(deptWorkshop);
        } else {
          deptWorkshopList.push(deptWorkshop);
        }
      }
      return deptWorkshopList;
    });
  },

  getDealerList: function () {
    return ajaxRequest('companyMaterial/getProviderNameAndShoppingCallCompany').then(function (dealerList) {
      var dealerObj = {};
      var allDealerObj = {};
      for (var i = 0; i < dealerList.length; i++) {
        var dealer = dealerList[i];
        dealer.dataId = `${dealer.id}-${dealer.source}`;
        dealer.isTax = dealer.isTax === 0;
        dealer.sourceText = i18n.getMsg('feddingBill.source.' + dealer.source);
        allDealerObj[dealer.dataId] = dealer;
        if (dealer.source === 2) {
          continue;
        }
        if (dealerObj[dealer.name] === undefined) {
          dealerObj[dealer.name] = dealer;
        }
      }
      return {
        dealerList: dealerList,
        dealerObj: dealerObj,
        allDealerObj: allDealerObj
      };
    });
  }
};
